package org.bsuir.filespocket.view;

import org.bsuir.filespocket.model.User;
import org.bsuir.filespocket.service.UserDAO;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;

public class AddUserController extends SimpleFormController {

    private UserDAO userDAO;

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        return new User();
    }

    @Override
    protected ModelAndView onSubmit(Object o) throws Exception {
        User newUser = (User) o;
        userDAO.saveUser(newUser);
        return new ModelAndView(new RedirectView(getSuccessView()));
    }
}
